<?php echo $header; ?>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?>
    	<?php if(isset($breadcrumb['href']) && $breadcrumb['href'] != '' && $breadcrumb['href'] != '#') {?>
	    	<a href="<?php echo $breadcrumb['href']; ?>">
	    		<?php echo $breadcrumb['text']; ?>
	    	</a>
    	<?php }else{?>
    		<?php echo $breadcrumb['text']; ?>
    	<?php }?>
    <?php } ?>
  </div>
  <?php if ($error_warning) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>
  <?php if ($success) { ?>
  <div class="success"><?php echo $success; ?></div>
  <?php } ?>
  <div class="box">
    <div class="heading">
      <h1><img src="view/image/news.png" alt="" /> <?php echo $heading_title; ?></h1>
      <div class="buttons">
      	<a onclick="location = '<?php echo $insert; ?>'" class="button"><?php echo $button_insert; ?></a>
      	<a onclick="$('form').submit();" class="button"><?php echo $button_delete; ?></a>
     </div>
    </div>
    <div class="content">
      <form action="<?php echo $delete; ?>" method="post" enctype="multipart/form-data" id="form">
      	<div style="float:right">
      		<select name="filter_type">
             	<option value="*">--<?php echo $text_all; ?>--</option>
                <?php foreach ($news_types as $news_type) { ?>
                <?php if ($news_type['type_id'] == $filter_type) { ?>
                <option value="<?php echo $news_type['type_id']; ?>" selected="selected"><?php echo $news_type['name']; ?></option>
                <?php } else { ?>
                <option value="<?php echo $news_type['type_id']; ?>" ><?php echo $news_type['name']; ?></option>
                <?php } ?>
                <?php } ?>
            </select>
      	</div>
      	<br/>&nbsp;
        <table class="list">
          <thead>
            <tr>
              <td width="1" style="text-align: center;">
              	<input type="checkbox" onclick="$('input[name*=\'selected\']').attr('checked', this.checked);" />
              </td>
              <td class="center" style="width: 100px"><?php echo $column_image; ?></td>
              <td class="left"><?php if ($sort == 'rnad.title') { ?>
                <a href="<?php echo $sort_title; ?>" class="<?php echo strtolower($order); ?>"><?php echo $common_news_title; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_title; ?>"><?php echo $common_news_title; ?></a>
                <?php } ?></td>
              <td class="left" style="width: 150px"><?php if ($sort == 'rncd.name') { ?>
                <a href="<?php echo $sort_category; ?>" class="<?php echo strtolower($order); ?>"><?php echo $common_category; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_category; ?>"><?php echo $common_category; ?></a>
                <?php } ?>
              </td>
              <td class="center" style="width: 100px"><?php if ($sort == 'rna.date_added') { ?>
                <a href="<?php echo $sort_date; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_date; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_date; ?>"><?php echo $column_date; ?></a>
                <?php } ?>
              </td>
              <td class="center" style="width: 100px"><?php if ($sort == 'rna.status') { ?>
                <a href="<?php echo $sort_status; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_status; ?></a>
                <?php } else { ?>
                <a href="<?php echo $sort_status; ?>"><?php echo $column_status; ?></a>
                <?php } ?>
              </td>
              <td class="center" style="width: 100px"><?php echo $column_action; ?></td>
            </tr>
          </thead>
          <tbody>
            <tr class="filter">
              <td></td>
              <td></td>
              <td><input type="text" name="filter_title" value="<?php echo $filter_title; ?>" /></td>
			  <td class="left">
			  	<select name="filter_category">
                  <option value="*"><?php echo $text_none; ?></option>
                  <?php foreach ($categories as $category) { ?>
                  <?php if ($category['category_id'] == $filter_category) { ?>
                  <option value="<?php echo $category['category_id']; ?>" selected="selected"><?php echo $category['name']; ?></option>
                  <?php } else { ?>
                  <option value="<?php echo $category['category_id']; ?>" ><?php echo $category['name']; ?></option>
                  <?php } ?>
                  <?php } ?>
                </select>
			  </td>
              <td class="center">
              	<input type="text" name="filter_month" value="<?php echo $filter_month; ?>" class="date" size="5" readonly="readonly"/>
              	<a><img title="<?php echo $button_delete;?>" src="view/image/delete.png" alt="" id="remove_filter_month"/></a>
              </td>
              <td class="center">
			  	<select name="filter_status">
                  <option value="*"><?php echo $text_none; ?></option>
                  <?php foreach ($status_list as $status) { ?>
                  <?php if ($status['value'] == $filter_status) { ?>
                  <option value="<?php echo $status['value']; ?>" selected="selected"><?php echo $status['name']; ?></option>
                  <?php } else { ?>
                  <option value="<?php echo $status['value']; ?>" ><?php echo $status['name']; ?></option>
                  <?php } ?>
                  <?php } ?>
                </select>
			  </td>
              <td class="center"><a onclick="filter();" class="button"><?php echo $button_filter; ?></a></td>
            </tr>            
            <?php if ($news_articles) { ?>
            <?php foreach ($news_articles as $news_article) { ?>
            <tr>
              <td style="text-align: center;"><?php if ($news_article['selected']) { ?>
                <input type="checkbox" name="selected[]" value="<?php echo $news_article['article_id']; ?>" checked="checked" />
                <?php } else { ?>
                <input type="checkbox" name="selected[]" value="<?php echo $news_article['article_id']; ?>" />
                <?php } ?>
              </td>
              <td class="center"><img src="<?php echo $news_article['image']; ?>" alt="<?php echo $news_article['title']; ?>" style="padding: 1px; border: 1px solid #DDDDDD;" /></td>
              <td class="left"><?php echo $news_article['title']; ?></td>
              <td class="left"><?php echo $news_article['category_name']; ?></td>
              <td class="center"><?php echo $news_article['date_added']; ?></td>
              <td class="center"><?php echo $news_article['status']; ?></td>
              <td class="center"><?php foreach ($news_article['action'] as $action) { ?>
                [ <a href="<?php echo $action['href']; ?>"><?php echo $action['text']; ?></a> ]
                <?php } ?>
              </td>
            </tr>
            <?php } ?>
            <?php } else { ?>
            <tr>
              <td class="center" colspan="9"><?php echo $text_no_results; ?></td>
            </tr>
            <?php } ?>
          </tbody>
        </table>
      </form>
      <div class="pagination"><?php echo $pagination; ?></div>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
function filter() {
	url = 'index.php?route=news/news&token=<?php echo $token; ?>';

	var filter_type = $('select[name=\'filter_type\']').val();	
	if (filter_type) {
		url += '&filter_type=' + encodeURIComponent(filter_type);
	}
	
	var filter_title = $('input[name=\'filter_title\']').val();	
	if (filter_title) {
		url += '&filter_title=' + encodeURIComponent(filter_title);
	}
	
	var filter_category = $('select[name=\'filter_type\']').val();	
	if (filter_category != '*') {
		url += '&filter_type=' + encodeURIComponent(filter_type);
	}
	
	var filter_category = $('select[name=\'filter_category\']').val();	
	if (filter_category != '*') {
		url += '&filter_category=' + encodeURIComponent(filter_category);
	}
	
	var filter_month = $('input[name=\'filter_month\']').val();	
	if (filter_month) {
		url += '&filter_month=' + encodeURIComponent(filter_month);
	}
	
	var filter_status = $('select[name=\'filter_status\']').val();	
	if (filter_status != '*') {
		url += '&filter_status=' + encodeURIComponent(filter_status);
	}

	<?php if(isset($this->request->get['sort'])){?>
		url += '&sort=<?php echo $this->request->get['sort']; ?>';
	<?php }?>

	<?php if(isset($this->request->get['order'])){?>
		url += '&order=<?php echo $this->request->get['order']; ?>';
	<?php }?>
	
	location = url;
}
//--></script> 
<script type="text/javascript"><!--
$(document).ready( function() {
	$('input[name=\'filter_title\']').focus();

	$('select[name=\'filter_type\']').change(function(){
		filter();
	});

	$('#remove_filter_month').click(function(){
		$('input[name=\'filter_month\']').val('');
	});
	
	$('#form input').keydown(function(e) {
		if (e.keyCode == 13) {
			filter();
		}
	});

	$('input[name=\'filter_title\']').autocomplete({
		delay: 0,
		source: function(request, response) {
			$.ajax({
				url: 'index.php?route=news/news/autocompleteTitle&token=<?php echo $token; ?>&filter_title=' +  encodeURIComponent(request.term),
				dataType: 'json',
				success: function(json) {		
					response($.map(json, function(item) {
						return {
							label: item.lable,
							value: item.value
						}
					}));
				}
			});
		}, 
		select: function(event, ui) {
			$('input[name=\'filter_title\']').val(ui.item.label);							
			return false;
		}
	});

	// Datetime picker
	$('.date').datepicker({ changeMonth: true,
	        changeYear: true,
	        showButtonPanel: true,
	        dateFormat: 'mm/yy',
	        onClose: function(dateText, inst) { 
	            var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
	            var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
            	$(this).datepicker('setDate', new Date(year, month, 1));
	        }
	});
});
//--></script> 
<style>
.ui-datepicker-calendar {
    display: none;
    }
</style>
<?php echo $footer; ?>